package com.ltf.controller.adm;

import com.ltf.entity.Pag;
import com.ltf.entity.XJson;
import com.ltf.entity.XUser;
import com.ltf.service.LargeFileService;
import com.ltf.service.XUserService;
import com.ltf.utils.PackJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

import java.util.List;

//学生管理
@RestController
@RequestMapping("/api/adm/stu")
public class StuController {
    //打印输出换成这个
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    XUserService xUserService;
    @Autowired
    LargeFileService largeFileService;

    public StuController() {
    }

    //分页显示
    @RequestMapping("/findByPage")
    public XJson findByPage(Pag pag, Integer pagenum, Integer lines) {
        Map<String, Object> m = xUserService.findBypag(pag, pagenum, lines);
        return PackJson.packX(200, "", m);
    }


    @GetMapping("/findByRole")
    public XJson findByRole() {//查询所有学生
        List<XUser> users = this.xUserService.findByRole("stu");
        return PackJson.packX(200, "", users);
    }

    @GetMapping("/findByGrade/{grade}")
    public XJson findByGrade(@PathVariable("grade") Integer grade) {//查询该年级学生
        List<XUser> byGrade = xUserService.findByGrade(grade);
        return PackJson.packX(200, "", byGrade);
    }


    //根据id查询单个学生
    @GetMapping("/findById/{uid}")
    public XJson findByUid(@PathVariable("uid") Integer uid) {
        XUser stu = this.xUserService.findByUid(uid);
        if (stu != null) {
            return PackJson.packX(200, "", stu);
        }
        return PackJson.packX(401, "该学生不存在", "");
    }

    //添加学生
    @PostMapping("/add")
    public XJson add(XUser xUser,
                     @RequestParam(value = "mypic", required = false)
                     MultipartFile myfile) throws IOException {
        //设置添加对象为学生
        xUser.setRole("stu");
        //默认密码
        xUser.setPwd("111");
        //进行图片处理，不为空时
        if (myfile != null) {
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            if (xUser.getPic() != null && xUser.getPic().length() > 0) {
                this.largeFileService.delete(xUser.getPic());
            }
            //添加图片id
            xUser.setPic(uuid);
            this.largeFileService.add(uuid, myfile.getOriginalFilename(), myfile.getBytes());
        }
        this.xUserService.add(xUser);
        return PackJson.packX(200, "添加成功", "");
    }

    //修改学生信息
    @PostMapping("/update")
    public XJson update(XUser xUser,
                        @RequestParam(value = "mypic", required = false)
                        MultipartFile myfile) throws IOException {
        xUser.setRole("stu");
        if (myfile != null) {
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            if (xUser.getPic() != null && xUser.getPic().length() > 0) {
                this.largeFileService.delete(xUser.getPic());
            }
            xUser.setPic(uuid);
            this.largeFileService.add(uuid, myfile.getOriginalFilename(), myfile.getBytes());
        }
        this.xUserService.update(xUser);
        return PackJson.packX(200, "修改成功", "");
    }

    //删除学生
    @GetMapping("/del/{uid}")
    public XJson del(@PathVariable("uid") Integer uid) {
        //先将学生查出来
        XUser stu = this.xUserService.findByUid(uid);
        List<XUser> stus = this.xUserService.findByRole("stu");
        if (stu != null) {
            this.xUserService.del(uid);
            return PackJson.packX(200, "删除成功", stus);
        }
        return PackJson.packX(401, "不存在该学生", "");
    }

    //所有年级，
    @GetMapping("/findGrade")
    public XJson findByGrade() {
        List<Integer> g = this.xUserService.findGrade();
        //去重处理
        Set<Integer> grades = new HashSet<>();
        for (Integer s : g) {
            grades.add(s);
        }
        return PackJson.packX(200, "", grades);
    }
}
